Obtaining 3D trajectory from accelerometer data

This article shows how to track the trajectory of a smartphone in 3D space. The data for this was downloaded using the App AndroSensor for Android (outputs are set to SI units, recording interval is set to 0.01 seconds (100Hz), and the delimiter in the CSV file format is “ , ”).

Think of a phone maneuver that you would like to track in 3D (for simplicity, I suggest moving up and down for 2 seconds), and when ready - press record and stop. Export the CSV with the built in email export, or find the AndroSensor folder in your phone directory and manually export the CSV.

The steps involved are:

Now we will open the CSV in Python, and plot the data-streams for our experiment (Fig. 4).

Body to earth frame conversion

The phone’s accelerometer measures acceleration in reference to itself (the body frame) and we need to transform the body frame accelerations to an inertial, non-rotating frame ( the earth frame). This will make it possible to hold the phone in any orientation, and measure the correct acceleration vectors to calculate the phone’s trajectory in the earth frame.

title

The transformation is done with matrix algebra as shown below, where [X,Y,Z] are the body frame linear accelerations, and Rz,Ry,Rx are the rotation matrices for each axis in order to rotate the [X,Y,Z] over to earth’s [x,y,z] axes. The Euler angles, (ψ, θ, φ) correspond to the angles about the x,y,z, axes or pitch, roll, and yaw.

title

Double integrate the earth frame accelerations

Let us see how to convert body frame to earth frame. The transformation is done with matrix algebra as shown in Fig. 7, where [X,Y,Z] are the body frame linear accelerations, and Rz,Ry,Rx are the rotation matrices for each axis in order to rotate the [X,Y,Z] over to earth’s [x,y,z] axes. The Euler angles, (ψ, θ, φ) correspond to the angles about the x,y,z, axes or pitch, roll, and yaw.

Next we will calculate the phone’s motion by integrating the linear accelerations and plot the results.

Drawing

For this motion, I raised and lowered my phone without it moving much in the X or Y direction — yet the trajectory shows that I moved over 5 m away from my starting point! What gives?

Performing a Fourier analysis of the acceleration measurements

The MEMS accelerometer in your phone is pretty cheap, and thus prone to generating a lot of noise. When we integrate the accelerations, we also integrate the noise — which causes a drift in the calculated trajectory. Most of the noise is usually at a low frequency, so it is possible to use a high-pass filter to attenuate the signal below this frequency. To determine the cutoff frequency, lets do a Fourier analysis of the noise spectrum and plot the results.

For each axis, we see that that there is quite a bit of noise below 10Hz. When designing the high-pass filter, we need to be careful to not attenuate frequencies that contain information about the movement of the phone. For Example, there is a large spike around 1Hz in the Z axis, and this might correspond to movement of the phone since I was moving it up and down at around 1Hz.

Create a high-pass filter to attenuate low frequency noise

Lets design a high-pass filter like this: Attenuate noise < 10 Hz in the X and Y axes by 10 dB (or multiply signal by 0.1) Attenuate noise > 5 Hz and < 10 Hz in Z axis by 10 dB

Make a 3D plot trajectory, and add x,y,z axis vectors to indicate phone pose.

Let’s compare the attenuated trajectory to the previous trajectory. The attenuated trajectory shows less than 1 m drift in the x,y plane! To finish this project, let’s add body axis arrows to the phone trajectory to indicate the phone’s orientation in space. This involves a reverse transformation from the earth frame to the body frame. We need to transform the unit earth x,y,z vectors ([1,0,0],[0,1,0],[0,0,1]) to body frame x,y,z unit vectors.

Quiver plot

Next we will build quiver plots to plot sets of arrows that indicate the phone’s [X,Y,Z] axes onto the trajectory. To make the plots easier to visualize, I will plot only one-tenth of the arrows (using [::10] slice notation).

References

Exploring Data Acquisition and Trajectory Tracking with Android Devices and Python, Mack Raymond.